package com.pan.admin.module.channel.controller;

import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.common.utils.poi.ExcelUtil;
import com.pan.admin.module.channel.entity.LoanChannelRegisterField;
import com.pan.admin.module.channel.entity.vo.ElementTreeVO;
import com.pan.admin.module.channel.service.LoanChannelRegisterFieldService;
import com.pan.admin.module.loanConfig.entity.LoanRegisterField;
import com.pan.admin.module.loanConfig.service.LoanRegisterFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 渠道关联注册字段配置Controller
 *
 * @author ts
 * @date 2023-09-18
 */
@RestController
@RequestMapping("/channel/RegisterField")
public class LoanChannelRegisterFieldController extends BaseController {

    @Autowired
    private LoanChannelRegisterFieldService loanChannelRegisterFieldService;

    @Autowired
    private LoanRegisterFieldService loanRegisterFieldService;

    /**
     * 查询渠道关联注册字段配置列表
     */
    @PreAuthorize("@ss.hasPermi('channel:registerField')")
    @GetMapping("/list")
    public TableDataInfo list(LoanChannelRegisterField loanChannelRegisterField) {
        startPage();
        List<LoanChannelRegisterField> list = loanChannelRegisterFieldService.list(loanChannelRegisterField);
        return getDataTable(list);
    }

    /**
     * 查询注册要素字段配置树控列表
     */
    @PreAuthorize("@ss.hasPermi('channel:registerField')")
    @GetMapping("/treeList")
    public TableDataInfo treeList(LoanRegisterField loanRegisterField) {
        List<ElementTreeVO> list = loanRegisterFieldService.treeList(loanRegisterField);
        return getDataTable(list);
    }

    /**
     * 查询渠道关联注册字段配置列表
     */
    @PreAuthorize("@ss.hasPermi('channel:registerField')")
    @GetMapping("/choose/treeList")
    public TableDataInfo treeList(LoanChannelRegisterField loanChannelRegisterField) {
        List list = loanChannelRegisterFieldService.treeList(loanChannelRegisterField);
        return getDataTable(list);
    }

    /**
     * 导出渠道关联注册字段配置列表
     */
    @PreAuthorize("@ss.hasPermi('channel:registerField:export')")
    @Log(title = "渠道关联注册字段配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LoanChannelRegisterField loanChannelRegisterField) {
        List<LoanChannelRegisterField> list = loanChannelRegisterFieldService.list(loanChannelRegisterField);
        ExcelUtil<LoanChannelRegisterField> util = new ExcelUtil<LoanChannelRegisterField>(LoanChannelRegisterField.class);
        util.exportExcel(response, list, "渠道关联注册字段配置数据");
    }

    /**
     * 获取渠道关联注册字段配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('channel:registerField')")
    @GetMapping(value = "/{name}")
    public AjaxResult getInfo(@PathVariable("name") String name) {
        return success(loanChannelRegisterFieldService.getById(name));
    }

    /**
     * 新增渠道关联注册字段配置
     */
    @PreAuthorize("@ss.hasPermi('channel:registerField')")
    @Log(title = "渠道关联注册字段配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanChannelRegisterField loanChannelRegisterField) {
        return toAjax(loanChannelRegisterFieldService.insertLoanChannelRegisterField(loanChannelRegisterField));
    }

    /**
     * 新增树控节点
     */
    @PreAuthorize("@ss.hasPermi('channel:registerField')")
    @Log(title = "渠道关联注册字段配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addTree(@RequestBody List<LoanChannelRegisterField> loanChannelRegisterField) {
        return toAjax(loanChannelRegisterFieldService.addTree(loanChannelRegisterField));
    }

    /**
     * 删除树控节点
     */
    @PreAuthorize("@ss.hasPermi('channel:registerField')")
    @Log(title = "渠道关联注册字段配置", businessType = BusinessType.INSERT)
    @PostMapping("/delete")
    public AjaxResult delTree(@RequestBody List<LoanChannelRegisterField> loanChannelRegisterField) {
        return toAjax(loanChannelRegisterFieldService.delTree(loanChannelRegisterField));
    }

    /**
     * 修改渠道关联注册字段配置
     */
    @PreAuthorize("@ss.hasPermi('channel:registerField')")
    @Log(title = "渠道关联注册字段配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanChannelRegisterField loanChannelRegisterField) {
        return toAjax(loanChannelRegisterFieldService.updateLoanChannelRegisterField(loanChannelRegisterField));
    }

    /**
     * 删除渠道关联注册字段配置
     */
    @PreAuthorize("@ss.hasPermi('channel:registerField')")
    @Log(title = "渠道关联注册字段配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(loanChannelRegisterFieldService.batchDeleteById(ids));
    }
}
